package rpg.writer.app.player;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JLabel;
import javax.swing.JPanel;

import rpg.client.Instruction;
import rpg.client.model.ActorInfo;
import rpg.client.model.ClientMap;
import rpg.client.model.Facing;
import rpg.client.model.Position;

public class MapJPanel extends JPanel implements KeyListener {
    /**
     * 
     */
    private static final long serialVersionUID = -552661103073463664L;

    private static void debug(Object o) {
        System.out.println("MapJPanel: " + o);
    }

    private volatile Communication communication = null;
    private volatile String playerID;
    private final PlayerJPanel player;

    public MapJPanel(PlayerJPanel pjp) {
        super();
        player = pjp;
        add(new JLabel("MAP"));
        addKeyListener(this);
    }

    private String describe(Facing facing) {
        double angle = 180 * facing.getHorizontal() / Math.PI;
        double d = angle % 360;
        int i = (int) d;
        if (i < 45) {
            return "EAST";
        }
        else if (i < 135) {
            return "NORTH";
        }
        else if (i < 215) {
            return "WEST";
        }
        else if (i < 305) {
            return "SOUTH";
        }
        return "EAST";
    }

    public void display(ClientMap map, Position playerPosition, Facing facing) {
        char[][] grid = new char[map.getRows().size()][];

        int row = 0;
        for (String s : map.getRows()) {
            grid[row] = s.toCharArray();
            row++;
        }

        // Add in the NPCs
        for (ActorInfo ai : map.getNpcs()) {
            setSafe(grid, ai.getPosition(), 'N');
        }

        // Add in the player
        setSafe(grid, playerPosition, '*');

        System.out.println(map.getName());
        System.out.println("Facing: " + describe(facing));
        for (char[] chars : grid) {
            System.out.println(new String(chars));
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        debug("released: " + e);
        Instruction ins = null;

        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
                ins = Instruction.turnLeft(playerID);
                break;
            case KeyEvent.VK_D:
                ins = Instruction.turnRight(playerID);
                break;
            case KeyEvent.VK_W:
                ins = Instruction.forward(playerID);
                break;
            case KeyEvent.VK_C:
                player.character();
                break;
            default:
                break;
        }

        if (ins != null) {
            if (communication != null) {
                communication.sendInstruction(ins);
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    public void setCommunication(Communication c, String pcID) {
        communication = c;
        playerID = pcID;
    }

    private void setSafe(char[][] grid, Position p, char c) {
        int x = (int) p.getX();
        int y = (int) p.getY();

        if (grid.length <= y) {
            return;
        }
        if (grid[y].length <= x) {
            return;
        }
        grid[y][x] = c;
    }
}
